package sparrowGui
{
	import flash.display.GradientType;
	import flash.geom.Matrix;
	
	import sparrowGui.utils.PubFun;

	/**
	 * @author Pelephone
	 * 基本组件的扩展，带自动绘制方法，用于皮肤skin的自动生成
	 * 有点类似于一个div
	 */	
	public class ComponentDraw extends Component
	{
		public function ComponentDraw(vars:Object=null)
		{
			vars = vars || SparrowGUI.getIns().componentVar;
			_skin = vars.bgImg;
			super(vars);
		}
		
		override protected function draw():void
		{
			PubFun.SafeRemoveChild(style.bgImg);
			if(style.bgShadowBlur) filters = [
				PubFun.getShadow(style.bgShadowDist,style.bgShadowBlur,style.bgShadowColor)];
			else filters = null;
			
			if(style.alpha){
				if((style.bgColor && !style.bgColor2) || style.bgColor==style.bgColor2 ){
					graphics.clear();
					graphics.beginFill(style.bgColor,style.alpha);
				}
				else if((style.bgColor && style.bgColor2) || style.bgColor!=style.bgColor2 ){
					var matr:Matrix = new Matrix();
					matr.createGradientBox(style.width, style.height, style.bgRotaion);
					graphics.beginGradientFill(GradientType.LINEAR,[style.bgColor,style.bgColor2],
						[style.alpha,style.alpha],[0x00, 0xFF],matr);
				}
				
				if(style.borderLeft==style.borderRight==style.borderTop==style.borderBottom && style.borderTop)
				{
					graphics.lineStyle(style.borderTop,style.borderTopColor);
					graphics.drawRoundRect(0,0,style.width,style.height,style.ellipse,style.ellipse);
				}
				else
				{
					if(style.borderLeft){
						graphics.lineStyle(style.borderLeft,style.borderLeftColor);
						graphics.moveTo(0,0);
						graphics.lineTo(0,(style.height));
					}
					if(style.borderRight){
						graphics.lineStyle(style.borderRight,style.borderRightColor);
						graphics.moveTo((0+style.width),0);
						graphics.lineTo((0+style.width),(0+style.height));
					}
					if(style.borderTop){
						graphics.lineStyle(style.borderTop,style.borderTopColor);
						graphics.moveTo(0,0);
						graphics.lineTo((0+style.width),0);
					}
					if(style.borderBottom){
						graphics.lineStyle(style.borderButtom,style.borderButtomColor);
						graphics.moveTo(0,(0+style.height));
						graphics.lineTo((0+style.width),(0+style.height));
					}
					if(style.bgColor) graphics.drawRect(0,0,style.width,style.height);
				}
				
				if(style.bgColor) graphics.endFill();
			}
			
			if(style.bgImg && !style.bgImg.parent){
				addChildAt(style.bgImg,0);
				PubFun.alignRect(this,style.bgImg,style.bgAlign);
			}
			
			if(style.isBgFill && style.bgImg){
				style.bgImg.width = style.width;
				style.bgImg.height = style.height;
			}
			
			super.draw();
		}
/*		override public function get width():Number
		{
			return style.width;
		}
		override public function get height():Number
		{
			return style.height;
		}*/
	}
}